<?php
namespace App\Process;

use EasySwoole\Component\Process\AbstractProcess;
use EasySwoole\Pool\Manager;
use Swoole\Coroutine;

class Consumer extends AbstractProcess {
    protected function run($arg)
    {
        go(function (){
            while (true) {

                //TODO:: 拿到redis
                /** @var $redis \EasySwoole\Redis\Redis*/
                $redis = Manager::getInstance()->get('redis')->defer();

                //TODO:: 从有序集合中拿到三秒(模拟30分钟)以前的订单
                $orderIds = $redis->zRangeByScore('delay_queue_test1', 0, time(), ['withscores' => TRUE]);

                if (empty($orderIds)) {
                    Coroutine::sleep(1);
                    continue;
                }

                //TODO::拿出后立马删除
                $redis->zRem('delay_queue_test1', ...$orderIds);

                foreach ($orderIds as $orderId)
                {
                    $data['order_id'] = $orderId;
                    $data['time'] = date("Y-m-d H:i:s");

                    var_dump($data);

                    //TODO::判断此订单30分钟后，是否仍未完成，做相应处理
                }
            }
        });
    }

}